//
//  XLGDownloadManager+ReviewFile.m
//  etionLib
//
//  Created by xqls on 12-5-17.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGDownloadManager+ReviewFile.h"

#import "XLGContext.h"

#import "FileTag.h"

@interface XLGDocumentInteractionController : UIDocumentInteractionController
{
    UIViewController *_parentvcl;
}

@property(nonatomic, weak) UIViewController *parentvcl;

@end

@implementation XLGDocumentInteractionController
@synthesize parentvcl = _parentvcl;

- (void)dealloc
{
    superdealloc_mm;
}

@end

@implementation XLGDownloadManager (ReviewFile)

//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
//
//-(int)OpenDownloadFile:(int)nEnterprisenumber filename:(NSString*)szFilename viewcontroller:(UIViewController*)vcl
//{
//    int nResult=FALSE;
//    NSString* s=[NSString stringWithFormat:@"%@/%@/%d/%@/%@",[XLGContext context].m_szUserpath,ENTD,nEnterprisenumber,DOWNLOADD,szFilename];
//    nResult=[XMSFileHelper isFileDirExist:s];
//    if(nResult==YES)
//    {
//        CDocumentInteractionController* docvcl=[CDocumentInteractionController new];
//        docvcl.m_parentvcl=vcl;
//        docvcl.delegate=self;
//        docvcl.URL=[NSURL fileURLWithPath:s];
//        [docvcl presentPreviewAnimated:YES];
//    }
//    return nResult;
//}
//
//#pragma clang diagnostic pop  

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

- (BOOL)openDownloadFinishedFile:(NSString *)fileName withTitle:(NSString*)title presentedViewController:(UIViewController *)vcl
{
    NSInteger nResult = FALSE;
    NSString *s = [NSString stringWithFormat:@"%@/%@", [self defaultStorePath], fileName];
    nResult = [XMSFileHelper isFileExist:s];
    if (nResult == YES)
    {
        XLGDocumentInteractionController *docvcl = [XLGDocumentInteractionController new];
        docvcl.parentvcl = vcl;
        docvcl.delegate = self;
        docvcl.URL = [NSURL fileURLWithPath:s];
        docvcl.name = title;
        docvcl.UTI = [self fileUTIBlackList:[fileName pathExtension]];
        [docvcl presentPreviewAnimated:YES];
    }
    return nResult;
}

- (NSString*)fileUTIBlackList:(NSString*)fileExt
{
    NSArray *exts = @[@"wps"];
    if([exts containsObject:fileExt] == NO)
        return nil;
    return @"public.text";
}

#pragma clang diagnostic pop  

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return ((XLGDocumentInteractionController *) controller).parentvcl;
}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [controller autorelease_mm];
}

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
{
    [controller autorelease_mm];
}

@end
